﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
using System.IO.Compression;

namespace CMS_GYM
{
    public class clsReportTemplatesDTO
    {
        #region "Variables And Properties"
        #region INT ReportTemplate - Mã số Report
        int _reportTemplate = 0;
        /// <summary>
        /// Mã số Report
        /// </summary>
        public int ReportTemplate
        {
            get { return _reportTemplate; }
            set { _reportTemplate = value; }
        }
        #endregion

        #region STRING ReportTemplateName - Tên Report
        string _reportTemplateName = string.Empty;
        /// <summary>
        /// Tên Report
        /// </summary>
        public string ReportTemplateName
        {
            get { return _reportTemplateName; }
            set { _reportTemplateName = value; }
        }
        #endregion

        #region STRING ReportFileName - Tên File Report
        string _reportFileName = string.Empty;
        /// <summary>
        /// Tên File Report
        /// </summary>
        public string ReportFileName
        {
            get { return _reportFileName; }
            set { _reportFileName = value; }
        }
        #endregion

        #region STRING ReportTemplateXml - Chuỗi XML định nghĩa Report
        string _reportTemplateXml = string.Empty;
        /// <summary>
        /// Chuỗi XML định nghĩa Report
        /// </summary>
        public string ReportTemplateXml
        {
            get
            {
                if (_reportTemplateXml == string.Empty) return string.Empty;
                else return DecompressString(_reportTemplateXml);
            }
            set
            {
                if (value == string.Empty) _reportTemplateXml = string.Empty;
                else _reportTemplateXml = CompressString(value);
            }
        }
        #endregion

        #region STRING Note - Ghi chú
        string _note = string.Empty;
        /// <summary>
        /// Ghi chú
        /// </summary>
        public string Note
        {
            get { return _note; }
            set { _note = value; }
        }
        #endregion

        #region BOOL ISINUSE - Còn sử dụng
        bool _isInUse = false;
        /// <summary>
        /// Còn sử dụng
        /// </summary>
        public bool IsInUse
        {
            get { return _isInUse; }
            set { _isInUse = value; }
        }
        #endregion
        #endregion

        #region "Initials"
        #region ReportTemplates()
        public clsReportTemplatesDTO()
        {
            _reportTemplate = 0;
            _reportTemplateName = string.Empty;
            _reportFileName = string.Empty;
            _reportTemplateXml = string.Empty;
            _note = string.Empty;
            _isInUse = false;
        }
        #endregion
        #endregion

        #region ReportTemplateDefinitionXML
        public TextReader ReportTemplateDefinitionXML
        {
            get
            {
                TextReader reader = new StringReader(DecompressString(_reportTemplateXml));
                try
                {
                    XmlDocument doc = null;
                    Stream str;
                    doc.Load(reader);
                    XmlNode node = null;
                    reader = new StringReader(doc.OuterXml);
                }
                catch { }
                return reader;
            }
        }
        #endregion

        #region "Compress And DeCompress String"
        #region CompressString(string text)
        public string CompressString(string text)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
            {
                zip.Write(buffer, 0, buffer.Length);
            }

            ms.Position = 0;
            MemoryStream outStream = new MemoryStream();

            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);

            byte[] gzBuffer = new byte[compressed.Length + 4];
            System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
            return Convert.ToBase64String(gzBuffer);
        }
        #endregion

        #region DecompressString(string compressedText)
        public string DecompressString(string compressedText)
        {
            byte[] gzBuffer = Convert.FromBase64String(compressedText);
            using (MemoryStream ms = new MemoryStream())
            {
                int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

                byte[] buffer = new byte[msgLength];

                ms.Position = 0;
                using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
                {
                    zip.Read(buffer, 0, buffer.Length);
                }

                return Encoding.UTF8.GetString(buffer);
            }
        }
        #endregion
        #endregion
    }
}
